package com.group_g.hkr;

import java.awt.CardLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class Panel_UserAuthentication extends JPanel implements ActionListener {
	private JButton logIn;
	private JTextField userName;
	private JPasswordField password;

	public Panel_UserAuthentication() {
		logIn = setUpLoginButton();

		this.add(logIn);
		this.setLayout(null);
		this.setBackground(Color.white);
		setUserNameAndPasswordLabels();
		userName = setUpUserNameField();
		password = setUpPasswordField();
		JLabel shopName = new JLabel("Shop Application");
		shopName.setFont(new Font("Arial", Font.ITALIC, 40));
		shopName.setBounds(350, 10, 400,150);
		this.add(shopName);
		this.add(userName);
		this.add(password);
		
	}
	
	private JButton setUpLoginButton(){
		JButton temp = new JButton("Log in");
		temp.setBounds(400, 500, 200, 50);
		temp.setFont(new Font("Sherif", Font.BOLD, 24));
		temp.addActionListener(this);
		return temp;
	}
	
	private void setUserNameAndPasswordLabels(){
		
		JLabel userN = new JLabel("User Name:");
		userN.setFont(new Font("Serif", Font.BOLD, 25));
		userN.setForeground(Color.BLACK);
		userN.setBounds(200, 200, 150, 50);
		
		JLabel pass = new JLabel("Password:");
		pass.setFont(new Font("Serif", Font.BOLD, 25));
		pass.setForeground(Color.BLACK);
		pass.setBounds(200, 300, 150, 50);
		
		this.add(userN);
		this.add(pass);
	}
	
	private JTextField setUpUserNameField(){
		final JTextField temp = new JTextField("Enter a User Name!");
		temp.setForeground(Color.GRAY.brighter());
		temp.setBounds(350, 200, 300, 50);
		temp.setFont(new Font("Serif", Font.ITALIC, 25));
		temp.addMouseListener(new MouseAdapter(){
            @Override
            public void mouseClicked(MouseEvent e){
            	temp.setFont(new Font("Serif", Font.BOLD, 25));
            	temp.setForeground(Color.BLACK);
                temp.setText("");
            }
        });
		return temp;
	}
	
	private JPasswordField setUpPasswordField(){
		final JPasswordField temp = new JPasswordField();
		temp.setFont(new Font("Serif", Font.BOLD, 25));
    	temp.setForeground(Color.BLACK);
		temp.setBounds(350, 300, 300, 50);
		return temp;
	}


	@Override
	public void actionPerformed(ActionEvent e) {
		DB_Connector connector = new DB_Connector();
		
		if (e.getSource() == logIn){
			if(connector.checkUser(userName.getText(),password.getText())) {
				userName.setText("");
				password.setText("");
				CardLayout cl = (CardLayout) GUI.getMainPanel().getLayout();
				cl.show(GUI.getMainPanel(), GUI.getShop());
			}
		}
		
	}

}
